﻿using CodeAngel.Domain;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace CodeAngel.UI
{
    class PropertyDefViewModel : ViewModel
    {

        #region Constructors

        public PropertyDefViewModel(PropertyDef thisPropertyDef)
        {
            this.PropertyDef = thisPropertyDef;
            this.PropertyDef.DataTypeProperty.ValueChanged += delegate { this.LoadForeignKeyPropertyDefs(); };
            this.InitializeDataTypes();
            this.LoadForeignKeyPropertyDefs();
            this.InitializeSqlDataTypes();
        }

        #endregion

        #region PropertyDef

        /// <summary>
        /// Gets or sets the PropertyDef.
        /// </summary>
        public PropertyDef PropertyDef
        {
            get { return this._PropertyDef; }
            set
            {
                if (value != this._PropertyDef)
                {
                    this._PropertyDef = value;
                    this.RaisePropertyChanged("PropertyDef");
                }
            }
        }
        private PropertyDef _PropertyDef;

        #endregion

        #region DataTypes

        /// <summary>
        /// Gets or sets the DataTypes.
        /// </summary>
        public List<string> DataTypes
        {
            get { return this._dataTypes; }
            private set
            {
                if (value != this._dataTypes)
                {
                    this._dataTypes = value;
                    this.RaisePropertyChanged("DataTypes");
                }
            }
        }
        private List<string> _dataTypes;

        private void InitializeDataTypes()
        {
            var Types = new List<string>();

            Types.Add("string");
            Types.Add("bool");
            Types.Add("DateTime");
            Types.Add("Guid");
            Types.Add("int");
            Types.Add("long");
            Types.Add("double");
            Types.Add("decimal");
            Types.Add("Byte[]");

            foreach (var ThisED in this.PropertyDef.EntityDef.Domain.EntityDefs.OrderBy(i => i.Name))
            {
                Types.Add(ThisED.Name);
                Types.Add(ThisED.ClassNames.Collection());
            }

            this.DataTypes = Types;
        }

        #endregion

        #region SqlDataTypes

        /// <summary>
        /// Gets or sets the SqlDataTypes.
        /// </summary>
        public List<string> SqlDataTypes
        {
            get { return this._sqlDataTypes; }
            set
            {
                if (value != this._sqlDataTypes)
                {
                    this._sqlDataTypes = value;
                    this.RaisePropertyChanged("SqlDataTypes");
                }
            }
        }
        private List<string> _sqlDataTypes;

        private void InitializeSqlDataTypes()
        {
            var Types = new List<string>();

            //Types.Add("BigInt");
            Types.Add("bit");
            //Types.Add("Char");
            Types.Add("datetime");
            //Types.Add("Decimal");
            //Types.Add("Float");
            //Types.Add("Image");
            Types.Add("int");
            //Types.Add("Money");
            //Types.Add("Real");
            Types.Add("Timestamp");
            Types.Add("uniqueidentifier");
            Types.Add("varcharmax");
            Types.Add("varbinarymax");
            //Types.Add("VarChar");

            this.SqlDataTypes = Types;
        }

        #endregion

        #region ForeignKeyPropertyDefs

        /// <summary>
        /// Gets or sets the ForeignKeyPropertyDefs.
        /// </summary>
        public ObservableCollection<PropertyDef> ForeignKeyPropertyDefs
        {
            get { return this._foreignKeyPropertyDefs; }
            private set
            {
                if (value != this._foreignKeyPropertyDefs)
                {
                    this._foreignKeyPropertyDefs = value;
                    this.RaisePropertyChanged("ForeignKeyPropertyDefs");
                }
            }
        }
        private ObservableCollection<PropertyDef> _foreignKeyPropertyDefs = new ObservableCollection<PropertyDef>();

        /// <summary>
        /// Initializes the available ForeignKeyPropertyDefs.
        /// </summary>
        private void LoadForeignKeyPropertyDefs()
        {
            if (this._foreignKeyPropertyDefs != null) this._foreignKeyPropertyDefs.Clear();
            if (this.PropertyDef.DataTypeEntityDef == null) return;

            // Load the possible foreign key Entities.
            foreach (var ThisPD in this.PropertyDef.DataTypeEntityDef.PropertyDefs.EntityProperties)
            {
                if (ThisPD.DataTypeEntityDef.Name == this.PropertyDef.EntityDef.Name)
                {
                    this._foreignKeyPropertyDefs.Add(ThisPD);
                }
            }
        }

        #endregion

    }
}
